package com.raedev.forms.items

import android.util.Log
import android.view.View
import com.raedev.forms.Const
import com.raedev.forms.FormType
import com.raedev.forms.R
import com.raedev.forms.dialog.CustomWheelPickerDialog
import com.raedev.forms.dialog.interfaces.OnCustomWheelPickerSelected
import com.raedev.forms.dict.FormDataProvider
import com.raedev.forms.dict.FormSelectItem
import com.raedev.forms.listener.FormSelectChangedListener
import com.raedev.forms.listener.FormViewHolder
import com.uduew.utils.ActivityUtils
import java.util.ArrayList

/**
 * 选择框表单
 * @author RAE
 * @date 2022/09/15
 * @copyright Copyright (c) https://github.com/raedev All rights reserved.
 */
class SelectFormItem(
    /** 表单数据提供 */
    var provider: FormDataProvider,
    label: String,
    name: String,
    value: String?,
    required: Boolean = false
) : FormItem(label, name, value), FormSelectChangedListener {

    override val layoutId: Int = R.layout.form_item_select
    override val formType: Int = FormType.Select.ordinal
    override var hint: String? = null
        get() = if (field == null) ActivityUtils.getTopActivity()
            .getString(R.string.form_select_hint) else field
    var dialogTitle: String? = null
        get() = if (field == null) ActivityUtils.getTopActivity()
            .getString(R.string.form_select_hint) + label else field

    /** 字典Key过滤 */
    var dictFilter: String? = null

    /** 是否显示全路径 */
    var enableFullPath: Boolean = true

    var dialog: CustomWheelPickerDialog? = null

    init {
        this.required = required
    }

    override fun onBindViewEvent(holder: FormViewHolder) {
        super.onBindViewEvent(holder)
        holder.itemView.setOnClickListener(this::onItemClick)
    }

    override fun onUnBindViewEvent(holder: FormViewHolder) {
        holder.itemView.setOnClickListener(null)
    }


    override fun onBindViewHolder(holder: FormViewHolder) {
        this.provider.filter = this.dictFilter
        val label = this.value?.let {
            if (enableFullPath) {
                provider.getItem(it)?.let { item -> provider.getFullLabel(item) }
            } else provider.getItemLabel(it)
        }
//        val textView = holder.getView<TextView>(R.id.tv_value)
        holder.setVisibility(R.id.tv_value, true)
        holder.setText(R.id.tv_value, label)
        holder.setHint(R.id.tv_value, this.hint, this.viewonly)

//        textListener?.onTextView(textView)

//        this.value.let {
//            if (it != null) {
//                val item = provider.getItem(it)
//                if (item != null) {
//                    onFormSelectChanged(item)
//                }
//            }
//        }
    }

    private fun onItemClick(view: View) {
        val manager = fragmentManager ?: return

        onCustomWheelPicker()
//        FormSelectDialogFragment.show(manager, this.value, this.dialogTitle).apply {
//            this.provider = this@SelectFormItem.provider
//            this.listener = this@SelectFormItem
//            this.provider.filter = this@SelectFormItem.dictFilter
//        }
    }


    var defaultCustomWheelSelect: IntArray = intArrayOf(0)
    var defaultSelectedItems: ArrayList<FormSelectItem> = ArrayList()

    fun onCustomWheelPicker() {
        val selectedItem = this.value.let {
            val value = it?.split(Const.CONNECTOR)
            provider.getItem(value?.first() ?: "")
        }
        var data = provider.getItems(selectedItem)
        var dataStr = data.toTypedArray()


        dialog = CustomWheelPickerDialog.build().setTitle(this.dialogTitle)
            .addWheel(dataStr)
            .setFormDataProvider(provider)
//            .setDefaultSelectedItems(defaultSelectedItems)
            .setDefaultSelect(0, defaultCustomWheelSelect[0])
            .show(object : OnCustomWheelPickerSelected() {
                fun refreshWheelData(item: FormSelectItem, picker: CustomWheelPickerDialog) {
                    if (!item.hasChildren) return
                    val children = item.children ?: return
                    var dataStr = children.toTypedArray()
                    picker.setWheel(0, dataStr)
                }

                override fun onSelected(
                    picker: CustomWheelPickerDialog,
                    item: FormSelectItem,
                    selectedTexts: Array<FormSelectItem?>?,
                    selectedIndex: IntArray,
                    selectedItems: ArrayList<FormSelectItem>
                ) {
                    defaultCustomWheelSelect = selectedIndex
                    defaultSelectedItems = selectedItems
                    onFormSelectChanged(item)
                    refreshWheelData(item, picker)
                }
            })

    }

    interface OnSelectedParameter {
        fun onSelectedParameter(item: String)
    }

    fun setDialogOk() {
        dialog?.okButton(object : OnCustomWheelPickerSelected() {
            fun refreshWheelData(item: FormSelectItem, picker: CustomWheelPickerDialog) {
                if (!item.hasChildren) return
                val children = item.children ?: return
                var dataStr = children.toTypedArray()
                picker.setWheel(0, dataStr)
            }

            override fun onSelected(
                picker: CustomWheelPickerDialog,
                text: FormSelectItem,
                selectedTexts: Array<FormSelectItem?>?,
                selectedIndex: IntArray,
                selectedItems: ArrayList<FormSelectItem>
            ) {
                defaultCustomWheelSelect = selectedIndex
                defaultSelectedItems = selectedItems

                onFormSelectChanged(text)
                refreshWheelData(text, picker)
            }
        })
    }

    var parameter: OnSelectedParameter = object : OnSelectedParameter {
        override fun onSelectedParameter(item: String) {

            setDialogOk()
        }
    }

    interface OnSelectedListener {
        fun onSelected(item: FormSelectItem, parameter: OnSelectedParameter)
    }

    var selectedListener: OnSelectedListener? = null


    fun setValueChanged(value: String) {
        onValueChanged(value, true)
    }

    override fun onFormSelectChanged(item: FormSelectItem) {
        if (!item.hasChildren) {
            selectedListener?.onSelected(item, parameter)
        }
        if (!item.isNext) {
            val newValue = provider.getItemValue(item.value)
            if (this.value != newValue) {
                onValueChanged(newValue, true)
            }
        }

    }
}